﻿using System;
using System.Web.UI.Design;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI;
using System.ComponentModel.Design;

namespace TPG.SharePoint.Workflow.Controls.Design
{
    [SupportsPreviewControl(true)]
    public class InputFormControlDesigner
        : ControlDesigner
    {
        protected InputFormControl Control
        {
            get { return (InputFormControl)Component; }
        }

        protected new InputFormControl ViewControl
        {
            get { return (InputFormControl)base.ViewControl; }
        }

        protected override bool UsePreviewControl
        {
            get { return true; }
        }

        public override DesignerActionListCollection ActionLists
        {
            get
            {
                DesignerActionListCollection actionLists = base.ActionLists;
                actionLists.Add(new InputFormControlActionList(this));
                return actionLists;
            }
        }

        public override TemplateGroupCollection TemplateGroups
        {
            get
            {
                TemplateGroupCollection groups = base.TemplateGroups;
                TemplateGroup headingsGroup = new TemplateGroup("Headings");
                headingsGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "LabelTemplate", Control, "LabelTemplate"));
                headingsGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "ExampleTemplate", Control, "ExampleTemplate"));
                groups.Add(headingsGroup);
                TemplateGroup contentGroup = new TemplateGroup("Content");
                contentGroup.AddTemplateDefinition(
                    new TemplateDefinition(this, "ControlTemplate", Control, "ControlTemplate"));
                groups.Add(contentGroup);
                return groups;
            }
        }

        public override void Initialize(System.ComponentModel.IComponent component)
        {
            if (component as InputFormControl == null)
            {
                throw new ArgumentException();
            }
            base.Initialize(component);
            SetViewFlags(ViewFlags.TemplateEditing, true);
        }

        public override string GetDesignTimeHtml(DesignerRegionCollection regions)
        {
            regions.Add(CreateTemplateRegion("ControlTemplate"));
            if (Control.LabelTemplate != null)
            {
                regions.Add(CreateTemplateRegion("LabelTemplate"));
            }
            if (Control.ExampleTemplate != null)
            {
                regions.Add(CreateTemplateRegion("ExampleTemplate"));
            }
            return GetDesignTimeHtml();
        }

        public override string GetDesignTimeHtml()
        {
            ((ICompositeControlDesignerAccessor)ViewControl).RecreateChildControls();
            IDictionary state = ((IControlDesignerAccessor)ViewControl).GetDesignModeState();
            int regionID = 0;
            AddRegionAttribute((WebControl)state["ControlTemplateContainer"], regionID++);
            if (Control.LabelTemplate != null)
            {
                AddRegionAttribute((WebControl)state["LabelTemplateContainer"], regionID++);
            }
            if (Control.ExampleTemplate != null)
            {
                AddRegionAttribute((WebControl)state["ExampleTemplateContainer"], regionID++);
            }
            SPDesignTimeStyleManager.ApplySharePointStyleRecursive(ViewControl);
            return base.GetDesignTimeHtml();
        }

        public override string GetEditableDesignerRegionContent(EditableDesignerRegion region)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            return DesignerRegionHelper.GetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region);
        }

        public override void SetEditableDesignerRegionContent(EditableDesignerRegion region, string content)
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.SetTemplatedEditableDesignerRegionContent(
                host, (TemplatedEditableDesignerRegion)region, content);
            ViewControlCreated = false;
        }


        void AddRegionAttribute(WebControl control, int regionID)
        {
            control.Controls.Clear();
            control.Attributes.Add(DesignerRegion.DesignerRegionAttributeName, regionID.ToString());
        }

        DesignerRegion CreateTemplateRegion(string regionName)
        {
            TemplateDefinition template = new TemplateDefinition(
                    this, regionName, Control, regionName);
            TemplatedEditableDesignerRegion region =
                new TemplatedEditableDesignerRegion(template);
            region.IsSingleInstanceTemplate = true;
            return region;
        }

        void ConvertToLabelTemplate()
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.ConvertToTemplate(
                host, "LabelTemplate", Control, "LabelTemplateContainer");
            ViewControlCreated = false;
        }

        void ResetLabelTemplate()
        {
            DesignerRegionHelper.ResetTemplate("LabelTemplate", Control);
        }

        void ConvertToExampleTemplate()
        {
            IDesignerHost host = (IDesignerHost)GetService(typeof(IDesignerHost));
            DesignerRegionHelper.ConvertToTemplate(
                host, "ExampleTemplate", Control, "ExampleTemplateContainer");
            ViewControlCreated = false;
        }

        void ResetExampleTemplate()
        {
            DesignerRegionHelper.ResetTemplate("ExampleTemplate", Control);
        }

        class InputFormControlActionList
            : DesignerActionList
        {
            InputFormControlDesigner _designer;

            public InputFormControlActionList(InputFormControlDesigner designer)
                : base(designer.Component)
            {
                _designer = designer;
            }

            public override DesignerActionItemCollection GetSortedActionItems()
            {
                DesignerActionItemCollection items = new DesignerActionItemCollection();
                if (_designer.InTemplateMode == false)
                {
                    if (_designer.Control.LabelTemplate == null)
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ConvertToLabelTemplate", "Convert to label template", true));
                    }
                    else
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ResetLabelTemplate", "Reset label template", true));
                    }
                    if (_designer.Control.ExampleTemplate == null)
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ConvertToExampleTemplate", "Convert to example template", true));
                    }
                    else
                    {
                        items.Add(new DesignerActionMethodItem(
                            this, "ResetExampleTemplate", "Reset label template", true));
                    }
                }
#if DEBUG
                items.Add(new DesignerActionMethodItem(
                    this, "GetHtml", "Get Html"));
#endif
                return items;
            }
#if DEBUG
            public void GetHtml()
            {
                HtmlPreviewer.ShowForDesigner(_designer);
            }
#endif

            public void ConvertToLabelTemplate()
            {
                _designer.ConvertToLabelTemplate();
            }

            public void ResetLabelTemplate()
            {
                _designer.ResetLabelTemplate();
            }

            public void ConvertToExampleTemplate()
            {
                _designer.ConvertToExampleTemplate();
            }

            public void ResetExampleTemplate()
            {
                _designer.ResetExampleTemplate();
            }
        }
    }
}
